home *** CD-ROM | disk | FTP | other *** search
/ Sprite 1984 - 1993 / Sprite 1984 - 1993.iso / src / cmds / as / dist / symbols.c < prev    next >
Encoding:
C/C++ Source or Header  |  1989-05-15  |  11.6 KB  |  427 lines

  1. /* symbols.c -symbol table-
  2.    Copyright (C) 1987 Free Software Foundation, Inc.
  3.  
  4. This file is part of GAS, the GNU Assembler.
  5.  
  6. GAS is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 1, or (at your option)
  9. any later version.
  10.  
  11. GAS is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. GNU General Public License for more details.
  15.  
  16. You should have received a copy of the GNU General Public License
  17. along with GAS; see the file COPYING.  If not, write to
  18. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
  19.  
  20.  
  21. #include "as.h"
  22. #include "hash.h"
  23. #include "obstack.h"        /* For "symbols.h" */
  24. #include "struc-symbol.h"
  25. #include "symbols.h"
  26. #include "frags.h"
  27.  
  28. #ifndef WORKING_DOT_WORD
  29. extern int new_broken_words;
  30. #endif
  31.  
  32. static
  33. struct hash_control *
  34. sy_hash;            /* symbol-name => struct symbol pointer */
  35.  
  36.                 /* Below are commented in "symbols.h". */
  37. unsigned int local_bss_counter;
  38. symbolS * symbol_rootP;
  39. symbolS * symbol_lastP;
  40. symbolS    abs_symbol;
  41. struct obstack    notes;
  42.  
  43.  
  44.  
  45. symbolS * symbol_find();    /* Keep C compiler happy. */
  46.  
  47. /*
  48.  * Un*x idea of local labels. They are made by "n:" where n
  49.  * is any decimal digit. Refer to them with
  50.  *  "nb" for previous (backward) n:
  51.  *  or "nf" for next (forward) n:.
  52.  *
  53.  * Like Un*x AS, we have one set of local label counters for entire assembly,
  54.  * not one set per (sub)segment like in most assemblers. This implies that
  55.  * one can refer to a label in another segment, and indeed some crufty
  56.  * compilers have done just that.
  57.  *
  58.  * I document the symbol names here to save duplicating words elsewhere.
  59.  * The mth occurence of label n: is turned into the symbol "Ln^Am" where
  60.  * n is a digit and m is a decimal number. "L" makes it a label discarded
  61.  * unless debugging and "^A"('\1') ensures no ordinary symbol SHOULD get the
  62.  * same name as a local label symbol. The first "4:" is "L4^A1" - the m
  63.  * numbers begin at 1.
  64.  */
  65.  
  66. typedef short unsigned int
  67. local_label_countT;
  68.  
  69. static local_label_countT
  70. local_label_counter[10];
  71.  
  72. static                /* Returned to caller, then copied. */
  73.   char symbol_name_build[12];    /* used for created names ("4f") */
  74.  
  75. #ifdef SUN_ASM_SYNTAX
  76. static int local_label_defined[10];
  77. #endif
  78.  
  79.  
  80. void
  81. symbol_begin()
  82. {
  83.   symbol_lastP = NULL;
  84.   symbol_rootP = NULL;        /* In case we have 0 symbols (!!) */
  85.   sy_hash = hash_new();
  86.   bzero ((char *)(& abs_symbol), sizeof(abs_symbol));
  87.   abs_symbol . sy_type = N_ABS;    /* Can't initialise a union. Sigh. */
  88.   bzero ((char *)(local_label_counter), sizeof(local_label_counter) );
  89.   local_bss_counter = 0;
  90. }
  91.  
  92. /*
  93.  *            local_label_name()
  94.  *
  95.  * Caller must copy returned name: we re-use the area for the next name.
  96.  */
  97.  
  98. char *                /* Return local label name. */
  99. local_label_name(n, augend)
  100.      register int n;    /* we just saw "n:", "nf" or "nb" : n a digit */
  101.      register int augend; /* 0 for nb, 1 for n:, nf */
  102. {
  103.   register char *    p;
  104.   register char *    q;
  105.   char symbol_name_temporary[10]; /* build up a number, BACKWARDS */
  106.  
  107.   know( n >= 0 );
  108.   know( augend == 0 || augend == 1 );
  109.   p = symbol_name_build;
  110.   * p ++ = 'L';
  111.   * p ++ = n + '0';        /* Make into ASCII */
  112.   * p ++ = 1;            /* ^A */
  113.   n = local_label_counter [ n ] + augend;
  114.                 /* version number of this local label */
  115.   /*
  116.    * Next code just does sprintf( {}, "%d", n);
  117.    * It is more elegant to do the next part recursively, but a procedure
  118.    * call for each digit emitted is considered too costly.
  119.    */
  120.   q = symbol_name_temporary;
  121.   for (*q++=0; n; q++)        /* emits NOTHING if n starts as 0 */
  122.     {
  123.       know(n>0);        /* We expect n > 0 always */
  124.       *q = n % 10 + '0';
  125.       n /= 10;
  126.     }
  127.   while ( * p ++ = * -- q )
  128.     {
  129.     }
  130.   /* The label, as a '\0' ended string, starts at symbol_name_build. */
  131.   return (symbol_name_build);
  132. }
  133.  
  134.  
  135. void
  136. local_colon (n)
  137.      int        n;    /* just saw "n:" */
  138. {
  139.   local_label_counter [n] ++;
  140. #ifdef SUN_ASM_SYNTAX
  141.   local_label_defined[n]=1;
  142. #endif
  143.   colon (local_label_name (n, 0));
  144. }
  145.  
  146. /*
  147.  *            symbol_new()
  148.  *
  149.  * Return a pointer to a new symbol.
  150.  * Die if we can't make a new symbol.
  151.  * Fill in the symbol's values.
  152.  * Add symbol to end of symbol chain.
  153.  *
  154.  *
  155.  * Please always call this to create a new symbol.
  156.  *
  157.  * Changes since 1985: Symbol names may not contain '\0'. Sigh.
  158.  */
  159.  
  160. symbolS *
  161. symbol_new (name, type, other, desc, value, frag)
  162.      char *        name;    /* We copy this: OK to alter your copy. */
  163.      unsigned char    type;    /* As in <a.out.h>. */
  164.      char        other;    /* As in <a.out.h>. */
  165.      short int        desc;    /* As in <a.out.h>. */
  166.      valueT        value;    /* As in <a.out.h>, often an address. */
  167.                 /* Often used as offset from frag address. */
  168.      struct frag *    frag;    /* For sy_frag. */
  169. {
  170.   register symbolS *        symbolP;
  171.   register char *        preserved_copy_of_name;
  172.   register unsigned int        name_length;
  173.            char *        p;
  174.  
  175.   name_length = strlen(name) + 1;
  176.   obstack_grow(¬es,name,name_length);
  177.   p=obstack_finish(¬es);
  178.   /* obstack_1done( ¬es, name, name_length, &p ); */
  179.   preserved_copy_of_name = p;
  180.   p=obstack_alloc(¬es,sizeof(struct symbol));
  181.   /* obstack_1blank( ¬es, sizeof(struct symbol), &p ); */
  182.   symbolP            = (symbolS *) p;
  183.   symbolP -> sy_name        = preserved_copy_of_name;
  184.   symbolP -> sy_type        = type;
  185.   symbolP -> sy_other        = other;
  186.   symbolP -> sy_desc        = desc;
  187.   symbolP -> sy_value        = value;
  188.   symbolP -> sy_frag        = frag;
  189.   symbolP -> sy_next        = NULL;    /* End of chain. */
  190.   symbolP -> sy_forward        = NULL; /* JF */
  191. #ifdef SUSPECT
  192.   symbolP -> sy_name_offset    = ~ 0; /* Impossible offset catches errors. */
  193.   symbolP -> sy_number        = ~ 0; /* Ditto. */
  194. #endif
  195.   /*
  196.    * Link to end of symbol chain.
  197.    */
  198.   if (symbol_lastP)
  199.     {
  200.       symbol_lastP -> sy_next = symbolP;
  201.     }
  202.   else
  203.     {
  204.       symbol_rootP = symbolP;
  205.     }
  206.   symbol_lastP = symbolP;
  207.  
  208.   return (symbolP);
  209. }
  210.  
  211. /*
  212.  *            colon()
  213.  *
  214.  * We have just seen "<name>:".
  215.  * Creates a struct symbol unless it already exists.
  216.  *
  217.  * Gripes if we are redefining a symbol incompatibly (and ignores it).
  218.  *
  219.  */
  220. void
  221. colon (sym_name)        /* just seen "x:" - rattle symbols & frags */
  222.      register char *  sym_name; /* symbol name, as a cannonical string */
  223.                 /* We copy this string: OK to alter later. */
  224. {
  225.   register struct symbol * symbolP; /* symbol we are working with */
  226.  
  227. #ifdef SUN_ASM_SYNTAX
  228.   /* Sun local labes go out of scope whenever a non-local symbol is
  229.      defined.  */
  230.  
  231.   if(*sym_name !='L')
  232.     bzero((void *)local_label_defined,sizeof(local_label_defined));
  233. #endif
  234.  
  235. #ifndef WORKING_DOT_WORD
  236.   if(new_broken_words) {
  237.     struct broken_word *a;
  238.     int possible_bytes;
  239.     fragS *frag_tmp;
  240.     char *frag_opcode;
  241.     extern md_short_jump_size;
  242.     extern md_long_jump_size;
  243.  
  244.     possible_bytes=md_short_jump_size+new_broken_words*md_long_jump_size;
  245.     frag_tmp=frag_now;
  246.     frag_opcode=frag_var(rs_broken_word,possible_bytes,possible_bytes,(relax_substateT)0,(symbolS *)broken_words,(long int)0,(char *)0);
  247.  
  248.     /* We want to store the pointer to where to insert the jump table in the
  249.        fr_opcode of the rs_broken_word frag.  This requires a little hackery */
  250.     while(frag_tmp && (frag_tmp->fr_type!=rs_broken_word || frag_tmp->fr_opcode))
  251.       frag_tmp=frag_tmp->fr_next;
  252.     know(frag_tmp);
  253.     frag_tmp->fr_opcode=frag_opcode;
  254.     new_broken_words = 0;
  255.  
  256.     for(a=broken_words;a && a->dispfrag==0;a=a->next_broken_word)
  257.       a->dispfrag=frag_tmp;
  258.   }
  259. #endif
  260.   if (symbolP = symbol_table_lookup( sym_name ))
  261.     {
  262. #ifdef    VMS
  263.       /*
  264.        *    If the new symbol is .comm AND it has a size of zero,
  265.        *    we ignore it (i.e. the old symbol overrides it)
  266.        */
  267.       if ((seg_N_TYPE [(int) now_seg] == (N_UNDF | N_EXT)) &&
  268.       ((obstack_next_free(& frags) - frag_now -> fr_literal) == 0))
  269.         return;
  270.       /*
  271.        *    If the old symbol is .comm and it has a size of zero,
  272.        *    we override it with the new symbol value.
  273.        */
  274.       if ((symbolP -> sy_type == (N_UNDF | N_EXT)) &&
  275.       (symbolP->sy_value == 0)) {
  276.           symbolP -> sy_frag  = frag_now;
  277.           symbolP -> sy_value = obstack_next_free(& frags) - frag_now -> fr_literal;
  278.           symbolP -> sy_type |= seg_N_TYPE [(int) now_seg]; /* keep N_EXT bit */
  279.           return;
  280.       }
  281. #endif    /* VMS */
  282.       /*
  283.        *    Now check for undefined symbols
  284.        */
  285.       if ((symbolP -> sy_type & N_TYPE) == N_UNDF)
  286.     {
  287.       if(   symbolP -> sy_other == 0
  288.          && symbolP -> sy_desc  == 0
  289.          && symbolP -> sy_value == 0)
  290.         {
  291.           symbolP -> sy_frag  = frag_now;
  292.           symbolP -> sy_value = obstack_next_free(& frags) - frag_now -> fr_literal;
  293.           know( N_UNDF == 0 );
  294.           symbolP -> sy_type |= seg_N_TYPE [(int) now_seg]; /* keep N_EXT bit */
  295.         }
  296.       else
  297.         {
  298. #ifdef    VMS
  299.           /*
  300.            *    There are still several cases to check:
  301.            *        A .comm/.lcomm symbol being redefined as
  302.            *            initialized data is OK
  303.            *        A .comm/.lcomm symbol being redefined with
  304.            *            a larger size is also OK
  305.            */
  306.           char New_Type = seg_N_TYPE [(int) now_seg];
  307.           if (((symbolP->sy_type == (N_UNDF | N_EXT)) ||
  308.            (symbolP->sy_type == N_BSS)) &&
  309.           (((New_Type & ~N_EXT) == N_DATA) ||
  310.            (New_Type == symbolP->sy_type))) {
  311.             /*
  312.              *    Select which of the 2 cases this is
  313.              */
  314.             if (New_Type == symbolP->sy_type) {
  315.                 /*
  316.                  *    If the new size is larger we just
  317.                  *    change its value.  If the new size
  318.                  *    is smaller, we ignore this symbol
  319.                  */
  320.                 if (symbolP->sy_value <
  321.                     (obstack_next_free(& frags) -
  322.                         frag_now -> fr_literal)) {
  323.                       symbolP -> sy_value = 
  324.                     obstack_next_free(& frags) -
  325.                         frag_now -> fr_literal;
  326.                 }
  327.             } else {
  328.                 /*
  329.                  *    It is a .comm/.lcomm being converted
  330.                  *    to initialized data.
  331.                  */
  332.                 symbolP -> sy_frag  = frag_now;
  333.                 symbolP -> sy_value = obstack_next_free(& frags) - frag_now -> fr_literal;
  334.                 symbolP -> sy_type |= seg_N_TYPE [(int) now_seg]; /* keep N_EXT bit */
  335.             }
  336.           } else {
  337. #endif    /* VMS */
  338.           as_fatal( "Symbol \"%s\" is already defined as \"%s\"/%d.%d.%d.",
  339.               sym_name,
  340.               seg_name [(int) N_TYPE_seg [symbolP -> sy_type & N_TYPE]],
  341.               symbolP -> sy_other, symbolP -> sy_desc,
  342.               symbolP -> sy_value);
  343. #ifdef    VMS
  344.         }
  345. #endif    /* VMS */
  346.         }
  347.     }
  348.       else
  349.     {
  350.       as_fatal("Symbol %s already defined.",sym_name);
  351.     }
  352.     }
  353.   else
  354.     {
  355.       symbolP = symbol_new (sym_name,
  356.                 (unsigned char)(seg_N_TYPE [(int) now_seg]),
  357.                 0,
  358.                 0,
  359.                 (valueT)(obstack_next_free(&frags)-frag_now->fr_literal),
  360.                 frag_now);
  361.       symbol_table_insert (symbolP);
  362.     }
  363. }
  364.  
  365.  
  366. /*
  367.  *            symbol_table_insert()
  368.  *
  369.  * Die if we can't insert the symbol.
  370.  *
  371.  */
  372.  
  373. void
  374. symbol_table_insert (symbolP)
  375.      struct symbol *    symbolP;
  376. {
  377.   register char *    error_string;
  378.  
  379.   know( symbolP );
  380.   know( symbolP -> sy_name );
  381.   if ( * (error_string = hash_jam (sy_hash, symbolP -> sy_name, (char *)symbolP)))
  382.     {
  383.       as_fatal( "Inserting \"%s\" into symbol table failed: %s",
  384.           symbolP -> sy_name, error_string);
  385.     }
  386. }
  387.  
  388. /*
  389.  *            symbol_find_or_make()
  390.  *
  391.  * If a symbol name does not exist, create it as undefined, and insert
  392.  * it into the symbol table. Return a pointer to it.
  393.  */
  394. symbolS *
  395. symbol_find_or_make (name)
  396.      char *    name;
  397. {
  398.   register symbolS *    symbolP;
  399.  
  400.   symbolP = symbol_table_lookup (name);
  401.   if (symbolP == NULL)
  402.     {
  403.       symbolP = symbol_new (name, N_UNDF, 0, 0, 0, & zero_address_frag);
  404.       symbol_table_insert (symbolP);
  405.     }
  406.   return (symbolP);
  407. }
  408.  
  409. /*
  410.  *            symbol_find()
  411.  * 
  412.  * Implement symbol table lookup.
  413.  * In:    A symbol's name as a string: '\0' can't be part of a symbol name.
  414.  * Out:    NULL if the name was not in the symbol table, else the address
  415.  *    of a struct symbol associated with that name.
  416.  */
  417.  
  418. symbolS *
  419. symbol_find (name)
  420.      char *    name;
  421. {
  422.   return ( (symbolS *) hash_find( sy_hash, name ));
  423. }
  424.  
  425.  
  426. /* end: symbols.c */
  427.